Data Sharing
Data sharing allows you to create two or more variables that share the same list, record, or script object data; it can be used to promote efficiency when working with large data structures. Only data in lists, records, and script objects can be shared; you cannot share other values. In addition, the shared structures must all be on the same computer.To create a variable that shares data with another variable whose value is a list, record, or script object, use the Set command. For example, the second Set command in the following example creates the variable
yourList
, which shares data with the previously defined variablemyList
.
set myList to { 1, 2, 3 } set yourList to myList --this command creates yourList, --which shares data with myList set item 1 of myList to 4 get yourList --result:{ 4, 2, 3}If you updatemyList
by setting the value of its first item to 4, then the value of bothmyList
andyourList
is{4, 2, 3}
. Rather than having multiple copies of shared data, the same data belongs to multiple structures. When one structure is updated, the other is automatically updated.To avoid data sharing for lists, records, and script objects, use the Copy command instead of the Set command. The Copy command makes a
copy of the list, record, or script object. Changing the value of the original changes does not change the value of the variable. Here's an example of
using Copy instead of Set to create the variableyourList
.
set myList to { 1, 2, 3 } copy myList to yourList --this command makes a copy of --mylist set item 1 of myList to 4 get yourList --result: { 1, 2, 3 }If you updatemyList
, the value ofyourList
is still{1, 2, 3}
.